package com.jc;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Author: HJC
 * @Date: 2025/11/3 20:59
 * @Description:
 **/
public class PostgreSQLQuery {
    public static void main(String[] args) {
        String url = null;
        String user = null;
        String password = null;
        String sql_str = "SELECT id, name, created_at FROM users";


        try (InputStream file = PostgreSQLQuery.class.getClassLoader().getResourceAsStream("db.properties")) {
            Properties pros = new Properties();
            pros.load(file);
            url = pros.getProperty("url");
            user = pros.getProperty("user");
            password = pros.getProperty("password");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        // 建立数据库连接，创建查询语句，并且执行语句
        try (Connection conn = DriverManager.getConnection(url, user, password);
             Statement statement= conn.createStatement();
             ResultSet rs=statement.executeQuery(sql_str)) {
            while (rs.next()) {
                System.out.println(rs.getInt("id") + " " + rs.getString("name") + " " + rs.getString("created_at"));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
